package com.sosee.cgfw.activity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.entity.JsonData;
import com.sosee.cgfw.myview.PickerView;
import com.sosee.cgfw.receiver.AlarmReceiver;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.umeng.analytics.MobclickAgent;
import com.zcw.togglebutton.ToggleButton;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Thinkpad on 2016/2/2.
 * 提醒activity
 */
public class RemindActivity extends Activity {
    private PickerView pv_day;//日期
    private PickerView pv_hour;//小时
    private PickerView pv_min;//分钟
    private List<String> day_list;//日期集合
    private List<String> hour_list;//小时 集合
    private List<String> time_list;//分集合
    private TextView tv_info;//标题
    private TextView tv_company;//举办方
    private TextView tv_company_e;
    private TextView tv_date;//日期
    private TextView tv_time;//时间
    private TextView tv_address;//地址
    private TextView tv_address_e;
    private String from;//判断从哪个页面跳转过来的 0scheduleActivity  1 scheduleDetailActivity
    private boolean is_Remind=true;
    private ToggleButton bt_Remind;


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remind);
        initData();
        initView();
        initDataTop();
    }
    //初始化view
    public void initView(){
        bt_Remind= (ToggleButton) findViewById(R.id.bt_remind);
        //设置提前通知按钮状态
        if(is_Remind){
            bt_Remind.setToggleOn();
        }else{
            bt_Remind.setToggleOff();
        }
        pv_day= (PickerView) findViewById(R.id.pv_day);
        pv_day.setData(day_list);
        pv_day.setSelected(10);
        pv_hour= (PickerView) findViewById(R.id.pv_hour);
        pv_hour.setData(hour_list);
        pv_hour.setSelected(10);
        pv_min= (PickerView) findViewById(R.id.pv_min);
        pv_min.setData(time_list);
        pv_min.setSelected(10);


        tv_info= (TextView) findViewById(R.id.tv_info);
        tv_address= (TextView) findViewById(R.id.tv_address);
        tv_address_e= (TextView) findViewById(R.id.tv_address_e);
        tv_company= (TextView) findViewById(R.id.tv_compan);
        tv_company_e= (TextView) findViewById(R.id.tv_company_e);
        tv_date= (TextView) findViewById(R.id.tv_date);
        tv_time= (TextView) findViewById(R.id.tv_start_time);
    }
    //初始化时间选择器数据
    public void initData(){
        hour_list=new ArrayList<String>();
        for (int i=1;i<25;i++){
            hour_list.add(i+"");
        }
        time_list=new ArrayList<String>();
        for (int i=0;i<60;i++){
            if(i<10){
                time_list.add("0"+i);
            }else{
                time_list.add(i+"");
            }
        }

        //待优化处理
        day_list= Utils.getTime();

    }
    private JsonData data;
    //初始化活动详情
    public void initDataTop(){
        from=getIntent().getStringExtra("from");
        data= (JsonData) getIntent().getSerializableExtra("data");
        String info=data.getData().getTitle();
        String company=data.getData().getUndertake();
        String company_e=data.getData().getUndertake_e();
        String date=data.getData().getDate();
        String time=data.getData().getStart_time();
        String address=data.getData().getAddressA();
        String address_e=data.getData().getAddressA_e();
//        String id=data.getData().getID();
        tv_info.setText(info);
        tv_address.setText(address);
        tv_address_e.setText(address_e);
        tv_company.setText(company);
        tv_company_e.setText(company_e);
        tv_date.setText(date);
        tv_time.setText(time);
    }
    //顶部按钮点击事件
    public void onClick(View view){
        int id=view.getId();
        LogUtils.log_E("onckick");
        switch (id){
            //返回键
            case R.id.ig_back:
                TurnToActivity();
                break;
            //完成设置
            case R.id.tv_success:
                setRemind();
                TurnToActivity();
                break;
        }
    }
    //跳转到其他activity
    public void TurnToActivity(){
        Intent intent;
        if("0".equals(from)){
            intent=new Intent(this,ScheduleActivity.class);
            startActivity(intent);
            finish();
        }else{
            intent=new Intent(this,ScheduleDetailActivity.class);
            intent.putExtra("detail",data);
            startActivity(intent);
            finish();
        }
    }
    //处理回退事件
    @Override
    public void onBackPressed() {
        TurnToActivity();
    }

    //设置提前通知
    public void setRemind(){
        Intent intent =new Intent(this, AlarmReceiver.class);
        intent.setAction("short");
        PendingIntent sender=
                PendingIntent.getBroadcast(this, 0, intent, 0);

    //设定一个五秒后的时间
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);

        AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
        alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

    }
}
